package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShortCarMapper;
import com.sky.service.ShortCarService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShortCarServiceImpl implements ShortCarService {

    @Autowired
    private ShortCarMapper shortCarMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<ShoppingCart> listByUserId() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shortCarMapper.queryByUserId(userId);
        return list;
    }

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1、查询该条数据是否存在
        if (shoppingCartDTO.getSetmealId() != null) {

            List<ShoppingCart> list = shortCarMapper.query(ShoppingCart.builder().setmealId(shoppingCartDTO.getSetmealId()).userId(BaseContext.getCurrentId()).build());

            if (list == null || list.size() <= 0) {
                //插入一条购物车数据
                ShoppingCart shoppingCart = new ShoppingCart();
                BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
                Setmeal setmeal = setmealMapper.queryById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setNumber(1);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shortCarMapper.insert(shoppingCart);

            } else {
                ShoppingCart shoppingCart = list.get(0);
                //更新购物车数据+1
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shortCarMapper.updateNumberBySetmealIdAndUserId(shoppingCart);

            }


        } else {
            List<ShoppingCart> list = shortCarMapper.query(ShoppingCart.builder().dishId(shoppingCartDTO.getDishId()).dishFlavor(shoppingCartDTO.getDishFlavor()).userId(BaseContext.getCurrentId()).build());

            if (list == null || list.size() <= 0) {
                //插入一条购物车数据
                ShoppingCart shoppingCart = new ShoppingCart();
                BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
                Dish dish = dishMapper.queryById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setNumber(1);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shortCarMapper.insert(shoppingCart);

            } else {
                ShoppingCart shoppingCart = list.get(0);
                //更新购物车数据+1
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shortCarMapper.updateByDishIdAndDishFlavorAndUserId(shoppingCart);

            }

        }


    }

    @Override
    public void cleanShoppingCartByUserId() {
        shortCarMapper.deleteByUserId(BaseContext.getCurrentId());

    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        if (shoppingCartDTO.getSetmealId() != null) {
            List<ShoppingCart> list = shortCarMapper.query(ShoppingCart.builder().setmealId(shoppingCartDTO.getSetmealId()).userId(BaseContext.getCurrentId()).build());
            if (list != null && list.size() > 0) {
                ShoppingCart shoppingCart = list.get(0);
                if (shoppingCart.getNumber() > 1) {
                    //更新购物车数据-1
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shortCarMapper.updateNumberBySetmealIdAndUserId(shoppingCart);
                }

            }
        } else {
            List<ShoppingCart> list = shortCarMapper.query(ShoppingCart.builder().dishId(shoppingCartDTO.getDishId()).dishFlavor(shoppingCartDTO.getDishFlavor()).userId(BaseContext.getCurrentId()).build());
            if (list != null && list.size() > 0) {
                ShoppingCart shoppingCart = list.get(0);
                if (shoppingCart.getNumber() > 1) {
                    //更新购物车数据-1
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shortCarMapper.updateByDishIdAndDishFlavorAndUserId(shoppingCart);
                }

            }
        }
    }
}
